﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using WinNemoLib;

namespace ToppingControl.Models
{
   public class TopCordPlanModel
   {
      public List<TopCordPlan_DS> SEL_TOPCORDPLN(string Noaction = "", string plandt = ""
                        , string Plansts="" ,string top ="",string seq ="",string unPlansts ="",string runflagNofin="")
      {
         string where = " and ";
         string ConnectionString = Nemo.GetConfiguredDbConnectionWrappertostring("Database");
         SqlConnection cnn = new SqlConnection(ConnectionString);
         StringBuilder str = new StringBuilder();

         str.AppendLine(" select " + top + " t.topno,t.itemno,t.topcrdcd topcrdcd,manufc,Plansts,t.plandt,length ,Uptdat,Uptemp ,irccrdcd ");
         str.AppendLine(",Tcrdlot,tcrdexp,Tcrdtagl,Tcrdtagw,tcrdw,tauthemp,treatxt,tshtxt,shift ");
         str.AppendLine(",isnull(t.Tcrdcd,b.Tcrdcd) Tcrdcd ,b.Irccrdcd,isnull(isnull(c.compcd,t.compcd),b.compcd) compcd ,Stdform ");
         str.AppendLine(",isnull(c.complot,t.Complot) complot, isnull(c.comppllt,t.comppllt) comppllt,isnull(c.compexp,t.Compexp) compexp,isnull(c.compw,t.compw) compw,t.Cauthemp,t.creatxt,t.cshtxt,runflag,finflag,actflag");
         str.AppendLine(" from " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDPLAN t");
         str.AppendLine(" LEFT JOIN " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDBOM b on t.topcrdcd=b.topcrdcd ");
         str.AppendLine(" LEFT JOIN ( select max(rollno)roll,topno,plandt,itemno,compcd,complot,comppllt,compexp,compw from  " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORD_CMPCHG  ");
         str.AppendLine(" group by topno,plandt,itemno,compcd,complot,comppllt,compexp,compw )c on t.topno=c.topno ");
         str.AppendLine(" and t.plandt=c.plandt and t.itemno=c.itemno ");
         str.AppendLine(" where Skpflag is null ");

         if (Noaction != "")
         {
            str.AppendLine(where + " isnull(actflag,'')='' and isnull([sample],'')='' ");
            where = " and ";
         }
         if (StrUtil.IsNotEmpty(plandt))
         {
            str.AppendLine(where + "  t.plandt=convert(datetime,@plndt,120) ");
            where = " and ";
         }
         if (StrUtil.IsNotEmpty(Plansts))
         {
            str.AppendLine(where + "  Plansts='" + Plansts +"'  ");
            where = " and ";
         }
         if (StrUtil.IsNotEmpty(unPlansts))
         {
            str.AppendLine(where + "  Plansts<>'" + unPlansts + "'");
            where = " and ";
         }
         if (StrUtil.IsNotEmpty(seq))
         {
            str.AppendLine(where + "  t.itemno=" + seq );
            where = " and ";
         }
         if (StrUtil.IsNotEmpty(runflagNofin))
         {
            str.AppendLine(where + "  runflag='" + runflagNofin + "' and isnull(finflag,'') = '' ");
            where = " and ";
         }

         SqlCommand cmd = new SqlCommand(str.ToString() + " order by t.plandt,convert(int,t.itemno) ", cnn);
         cmd.Connection.Open();
         cmd.CommandType = CommandType.Text;


         if (plandt != "") cmd.Parameters.Add("@plndt", SqlDbType.VarChar).Value = DateUtil.ToInternalDate(plandt, WinNemoLib.DateUtil.eLanguage.EN);
      
         SqlDataAdapter my = new SqlDataAdapter();
         DataSet ds = new DataSet();
         my.SelectCommand = cmd;
         my.Fill(ds);

         List<TopCordPlan_DS> list = new List<TopCordPlan_DS>();
         if (ds.Tables[0].Rows.Count <= 0)
         {
            cmd.Connection.Close();
            return list;
         }
         for (int cuIndex = 0; cuIndex < ds.Tables[0].Rows.Count; cuIndex++)
         {
            DataRow items = ds.Tables[0].Rows[cuIndex];
            TopCordPlan_DS model = new TopCordPlan_DS();
            model._Topno = items["topno"].ToString();
            model._Itemno = items["itemno"].ToString();
            model._Topcrdcd = items["topcrdcd"].ToString();
            model._Manufc = items["manufc"].ToString();
            model._Plants=items["Plansts"].ToString();
            model._Plandt = DateUtil.ToDisplayDate(items["plandt"],DateUtil.eLanguage.EN);
            model._Length = items["length"].ToString();
            model._Uptdat = DateUtil.ToDisplayDateTime(items["Uptdat"],DateUtil.eLanguage.EN);
            model._UpTemp = items["Uptemp"].ToString();
            model._Tcrdcd = items["Tcrdcd"].ToString();

            model._Tcrdlot = items["Tcrdlot"].ToString();
            model._Tcrdexp =  DateUtil.ToDisplayDate(items["tcrdexp"],DateUtil.eLanguage.EN);
            model._Tcrdtagl = items["Tcrdtagl"].ToString();
            model._Tcrdtagw = items["Tcrdtagw"].ToString();
            model._Tcrdw = items["tcrdw"].ToString();
            model._Tauthemp = items["tauthemp"].ToString();
            model._Tshtxt = items["tshtxt"].ToString();
            model._Treatxt = items["treatxt"].ToString();
            
     //    str.AppendLine(",Complot,Comppllt,Compexp,compw,Cauthemp,creatxt,cshtxt")

            model._Compcd = items["compcd"].ToString();
            model._Complot = items["Complot"].ToString();
            model._Comppllt = items["Comppllt"].ToString();
            model._Compexp= DateUtil.ToDisplayDate(items["Compexp"], DateUtil.eLanguage.EN);
            model._Compw = items["compw"].ToString();
            model._Cauthemp = items["Cauthemp"].ToString();
            model._Creatxt = items["creatxt"].ToString();
            model._Cshtxt = items["cshtxt"].ToString();       

            model._Stdform = items["Stdform"].ToString();
            model._Actflag = items["actflag"].ToString();
            model._Runflag = items["runflag"].ToString();
            model._Finflag = items["finflag"].ToString();
            model._Shift = items["shift"].ToString();
            model._irccrdcd = items["irccrdcd"].ToString();
            list.Add(model);
         }
         return list;
      }

      public List<TopCordPlan_DS> SEL_UNLOCK(string plandt)
      {
         string where = " where ";
         string ConnectionString = Nemo.GetConfiguredDbConnectionWrappertostring("Database");
         SqlConnection cnn = new SqlConnection(ConnectionString);
         StringBuilder str = new StringBuilder();
         
         if (StrUtil.IsEmpty(plandt)) plandt = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);

         str.AppendLine(" select  t.topno,itemno,t.topcrdcd topcrdcd,manufc,Plansts,plandt,length ,Uptdat,Uptemp");
         str.AppendLine(",Tcrdlot,tcrdexp,Tcrdtagl,Tcrdtagw,tcrdw,tauthemp,treatxt,tshtxt ");
         str.AppendLine(",isnull(t.Tcrdcd,b.Tcrdcd) Tcrdcd ,b.Irccrdcd,isnull(t.compcd,b.compcd) compcd ,Stdform ");
         str.AppendLine(",Complot,Comppllt,Compexp,compw,Cauthemp,creatxt,cshtxt,runflag,finflag,actflag");
         str.AppendLine(" from " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDPLAN t");
         str.AppendLine(" LEFT JOIN " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDBOM b on t.topcrdcd=b.topcrdcd ");
         str.AppendLine(where + " ( isnull(actflag,'') = ''  and  plandt=convert(datetime,@plndt,120) ) and isnull([sample],'')=''  ");     
      
        

         SqlCommand cmd = new SqlCommand(str.ToString() + " order by plandt,convert(int,itemno) ", cnn);
         cmd.Connection.Open();
         cmd.CommandType = CommandType.Text;


        cmd.Parameters.Add("@plndt", SqlDbType.VarChar).Value = DateUtil.ToInternalDate(plandt, WinNemoLib.DateUtil.eLanguage.EN);

         SqlDataAdapter my = new SqlDataAdapter();
         DataSet ds = new DataSet();
         my.SelectCommand = cmd;
         my.Fill(ds);

         List<TopCordPlan_DS> list = new List<TopCordPlan_DS>();
         if (ds.Tables[0].Rows.Count <= 0)
         {
            cmd.Connection.Close();
            return list;
         }
         for (int cuIndex = 0; cuIndex < ds.Tables[0].Rows.Count; cuIndex++)
         {
            DataRow items = ds.Tables[0].Rows[cuIndex];
            TopCordPlan_DS model = new TopCordPlan_DS();
            model._Topno = items["topno"].ToString();
            model._Itemno = items["itemno"].ToString();
            model._Topcrdcd = items["topcrdcd"].ToString();
            model._Manufc = items["manufc"].ToString();
            model._Plants = items["Plansts"].ToString();
            model._Plandt = DateUtil.ToDisplayDate(items["plandt"], DateUtil.eLanguage.EN);
            model._Length = items["length"].ToString();
            model._Uptdat = DateUtil.ToDisplayDateTime(items["Uptdat"], DateUtil.eLanguage.EN);
            model._UpTemp = items["Uptemp"].ToString();
            model._Tcrdcd = items["Tcrdcd"].ToString();

            model._Tcrdlot = items["Tcrdlot"].ToString();
            model._Tcrdexp = DateUtil.ToDisplayDate(items["tcrdexp"], DateUtil.eLanguage.EN);
            model._Tcrdtagl = items["Tcrdtagl"].ToString();
            model._Tcrdtagw = items["Tcrdtagw"].ToString();
            model._Tcrdw = items["tcrdw"].ToString();
            model._Tauthemp = items["tauthemp"].ToString();
            model._Tshtxt = items["tshtxt"].ToString();
            //    str.AppendLine(",Complot,Comppllt,Compexp,compw,Cauthemp,creatxt,cshtxt")

            model._Compcd = items["compcd"].ToString();
            model._Complot = items["Complot"].ToString();
            model._Comppllt = items["Comppllt"].ToString();
            model._Compexp = DateUtil.ToDisplayDate(items["Compexp"], DateUtil.eLanguage.EN);
            model._Compw = items["compw"].ToString();
            model._Cauthemp = items["Cauthemp"].ToString();
            model._Creatxt = items["creatxt"].ToString();
            model._Cshtxt = items["cshtxt"].ToString();

            model._Stdform = items["Stdform"].ToString();
            model._Actflag = items["actflag"].ToString();
            model._Runflag = items["runflag"].ToString();
            model._Finflag = items["finflag"].ToString();

            list.Add(model);
         }
         return list;
      }

      public List<TopCordPlan_DS> GET_TOPCORDPLN_ACT(string plandt = "" ,string top ="15",Boolean getSkip=false)
      {
         string where = " and ";
         string ConnectionString = Nemo.GetConfiguredDbConnectionWrappertostring("Database");
         SqlConnection cnn = new SqlConnection(ConnectionString);
         StringBuilder str = new StringBuilder();

         str.AppendLine(" select Top " + top + " t.topno,itemno,t.topcrdcd topcrdcd,manufc,Plansts,plandt,length ,Uptdat,Uptemp ,Skpflag, [sample],");
         str.AppendLine(" isnull(t.Tcrdcd,b.Tcrdcd) Tcrdcd  ,b.Irccrdcd,b.compcd,Stdform from " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDPLAN t");
         str.AppendLine(" LEFT JOIN " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDBOM b on t.topcrdcd=b.topcrdcd ");
         str.AppendLine(" Where (actflag='X' or finflag='X')  ");
         if(!getSkip) str.AppendLine(" and Skpflag is null ");
         if (StrUtil.IsNotEmpty(plandt))
         {
            str.AppendLine(where + "  plandt=convert(datetime,@plndt,120) ");
            where = " and ";
         }
 
   

         SqlCommand cmd = new SqlCommand(str.ToString() + " order by plandt,itemno ", cnn);
         cmd.Connection.Open();
         cmd.CommandType = CommandType.Text;


         if (plandt != "") cmd.Parameters.Add("@plndt", SqlDbType.VarChar).Value = DateUtil.ToInternalDate(plandt, WinNemoLib.DateUtil.eLanguage.EN);

         SqlDataAdapter my = new SqlDataAdapter();
         DataSet ds = new DataSet();
         my.SelectCommand = cmd;
         my.Fill(ds);

         List<TopCordPlan_DS> list = new List<TopCordPlan_DS>();
         if (ds.Tables[0].Rows.Count <= 0)
         {
            cmd.Connection.Close();
            return list;
         }
         for (int cuIndex = 0; cuIndex < ds.Tables[0].Rows.Count; cuIndex++)
         {
            DataRow items = ds.Tables[0].Rows[cuIndex];
            TopCordPlan_DS model = new TopCordPlan_DS();
            model._Topno = items["topno"].ToString();
            model._Itemno = items["itemno"].ToString();
            model._Topcrdcd = items["topcrdcd"].ToString();
            model._Manufc = items["manufc"].ToString();
            model._Plants = items["Plansts"].ToString();
            model._Plandt = DateUtil.ToDisplayDate(items["plandt"]);
            model._Length = items["length"].ToString();
            model._Uptdat = DateUtil.ToDisplayDateTime(items["Uptdat"].ToString());
            model._UpTemp = items["Uptemp"].ToString();
            model._Tcrdcd = items["Tcrdcd"].ToString();

            model._Compcd = items["compcd"].ToString();
            model._Stdform = items["Stdform"].ToString();
             model._Skpflag=items["Skpflag"].ToString();
             model._Sample = items["sample"].ToString();
            list.Add(model);
         }
         return list;
      }

      public List<TopCordPlan_DS> GET_NOT_FINISH(string plandt = "")
      {
         string where = " and ";
         string ConnectionString = Nemo.GetConfiguredDbConnectionWrappertostring("Database");
         SqlConnection cnn = new SqlConnection(ConnectionString);
         StringBuilder str = new StringBuilder();

         str.AppendLine(" select * from " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDPLAN t");
         str.AppendLine(" Where actflag='X' AND (isnull(runflag,'')='' or isnull(finflag,'')='') ");
         str.AppendLine(" and Skpflag is null ");
         if (StrUtil.IsNotEmpty(plandt))
         {
            str.AppendLine(where + "  plandt=convert(datetime,@plndt,120) ");
            where = " and ";
         }



         SqlCommand cmd = new SqlCommand(str.ToString() + " order by plandt,convert(int,itemno) ", cnn);
         cmd.Connection.Open();
         cmd.CommandType = CommandType.Text;


         if (plandt != "") cmd.Parameters.Add("@plndt", SqlDbType.VarChar).Value = DateUtil.ToInternalDate(plandt, WinNemoLib.DateUtil.eLanguage.EN);

         SqlDataAdapter my = new SqlDataAdapter();
         DataSet ds = new DataSet();
         my.SelectCommand = cmd;
         my.Fill(ds);

         List<TopCordPlan_DS> list = new List<TopCordPlan_DS>();
         if (ds.Tables[0].Rows.Count <= 0)
         {
            cmd.Connection.Close();
            return list;
         }
         for (int cuIndex = 0; cuIndex < ds.Tables[0].Rows.Count; cuIndex++)
         {
            DataRow items = ds.Tables[0].Rows[cuIndex];
            TopCordPlan_DS model = new TopCordPlan_DS();
            model._Topno = items["topno"].ToString();
            model._Itemno = items["itemno"].ToString();
            model._Topcrdcd = items["topcrdcd"].ToString();
            model._Manufc = items["manufc"].ToString();
            model._Plants = items["Plansts"].ToString();
            model._Plandt = DateUtil.ToDisplayDate(items["plandt"]);
            model._Length = items["length"].ToString();
            model._Uptdat = DateUtil.ToDisplayDateTime(items["Uptdat"].ToString());
            model._UpTemp = items["Uptemp"].ToString();
            model._Tcrdcd = items["Tcrdcd"].ToString();
            model._Compcd = items["compcd"].ToString();
            model._Empname = items["empname"].ToString();
            model._Actflag = items["actflag"].ToString();            
            model._Runflag = items["runflag"].ToString();
            model._Finflag = items["finflag"].ToString();
            model._Sample = items["sample"].ToString();


            model._Compcd = items["compcd"].ToString();
            model._Complot = items["Complot"].ToString();
            model._Comppllt = items["Comppllt"].ToString();
            model._Compexp = DateUtil.ToDisplayDate(items["Compexp"], DateUtil.eLanguage.EN);
            model._Compw = items["compw"].ToString();
            model._Cauthemp = items["Cauthemp"].ToString();
            model._Creatxt = items["creatxt"].ToString();
            model._Cshtxt = items["cshtxt"].ToString();       


            list.Add(model);
         }
         return list;
      }

      public int GET_SEQ(string plndt,string itemno="",Boolean isSample=false)
      {
         int seq = 0;
         string ConnectionString = Nemo.GetConfiguredDbConnectionWrappertostring("Database");
         SqlConnection cnn = new SqlConnection(ConnectionString);
         StringBuilder str = new StringBuilder();

         if (isSample)
         {
            str.AppendLine("SELECT ISNULL(MAX(ITEMNO),0)+1 seq_no from " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDPLAN ");
            str.AppendLine(" where  plandt=convert(datetime,@dTo,120)  AND ITEMNO BETWEEN CONVERT(INT,@It)-10 AND  CONVERT(INT,@It)-1 ");
            //str.AppendLine(" and Skpflag is null ");
            str.AppendLine(" group by plandt ");
         }
         else
         {
            str.AppendLine(" select isnull(max(itemno),0)+10 seq_no from " + Nemo.GetINI("Dbase", "Base") + ".dbo.TOPCORDPLAN ");
            str.AppendLine(" where (plansts ='L' and plandt=convert(datetime,@dTo,120))  ");
            str.AppendLine(" and Skpflag is null ");
            str.AppendLine(" group by plandt ");
         }

         SqlCommand cmd = new SqlCommand(str.ToString(), cnn);
         cmd.Connection.Open();
         cmd.CommandType = CommandType.Text;
         cmd.Parameters.Add("@dTo", SqlDbType.NVarChar).Value = DateUtil.ToInternalDate(plndt, WinNemoLib.DateUtil.eLanguage.EN);
         if (isSample)
         {
             cmd.Parameters.Add("@It", SqlDbType.NVarChar).Value = itemno;
         }
         SqlDataAdapter my = new SqlDataAdapter();
         DataSet ds = new DataSet();
         my.SelectCommand = cmd;
         my.Fill(ds);

         List<TopCordPlan_DS> list = new List<TopCordPlan_DS>();
         if (ds.Tables[0].Rows.Count <= 0)
         {
            cmd.Connection.Close();
               return 10;
         }
         seq = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
       
         return seq;
      }

      public class TopCordPlan_DS {
         public string _Topno { get; set; }
         public string _Plandt { get; set; }
         public string _Itemno { get; set; }
         public string _Topcrdcd { get; set; }
         public string _Manufc { get; set; }
         public string _Length { get; set; }
         public string _Plants { get; set; }
         public string _UpTemp { get; set; }
         public string _Uptdat { get; set; }
         public string _Actflag { get; set; }
         public string _Actdt { get; set; }
         public string _Runflag { get; set; }
         public string _Rundt { get; set; }
         public string _Empname { get; set; }
         public string _Shift { get; set; }
         public string _Finflag { get; set; }
         public string _Findt { get; set; }
         public string _Tcrdcd { get; set; }
         public string _irccrdcd { get; set; }
         public string _Tcrdlot { get; set; }
         public string _Tcrdexp { get; set; }
         public string _Tcrdtagl { get; set; }
         public string _Tcrdtagw { get; set; }
         public string _Tcrdw { get; set; }
         public string _Tauthemp { get; set; }
         public string _Treatxt { get; set; }
         public string _Tshtxt { get; set; }
         public string _Compcd { get; set; }
         public string _Complot { get; set; }
         public string _Comppllt { get; set; }
         public string _Compexp { get; set; }
         public string _Compw { get; set; }
         public string _Cauthemp { get; set; }
         public string _Creatxt { get; set; }
         public string _Cshtxt { get; set; }
         public string _Stdform { get; set; }
         public string _Sample { get; set; }
         public string _Skpflag { get; set; }
         public string _skpdt { get; set; }
         public string _skpemp { get; set; }
         public string _skpshtxt { get; set; }
         public string _smpemp { get; set; }
         public string _smpshtxt { get; set; }
      }

      public Boolean Ins_Data(ConnectionWrapper cnn, TopCordPlan_DS incoming, string usernm)
      {
         try
         {
            StringBuilder comstr = new StringBuilder();
            comstr.AppendLine(" INSERT INTO ");
            comstr.AppendLine(Nemo.GetINI("Dbase", "Base") + ".[dbo].[TOPCORDPLAN]");
            comstr.AppendLine(" (topno,shift,Plansts");
            if (StrUtil.IsNotEmpty(incoming._Sample))
            {// sample
               comstr.AppendLine(",runflag,rundat,actflag,actdt ");

               comstr.AppendLine(", [Tcrdcd], [Tcrdlot]");
               comstr.AppendLine(" , [tcrdexp], [Tcrdtagl], [Tcrdtagw]");
               comstr.AppendLine(" , [tcrdw]");

               comstr.AppendLine(" , [Compcd], [Complot]");
               comstr.AppendLine(" , [Comppllt], [Compexp], [compw] ,smpemp ,smpshtxt");               


            }
            comstr.AppendLine(",plandt,itemno,topcrdcd, manufc, length,Uptemp, Uptdat,[sample] ) VALUES ( @topno,@shift ,@status");
            if (StrUtil.IsNotEmpty(incoming._Sample))
            {
               comstr.AppendLine(",@runflag,getdate(),@actflag,getdate() ");

               comstr.AppendLine(",@Tcrdcd,@Tcrdlot");
               comstr.AppendLine(" ,convert(datetime,@tcrdexp,120) , @Tcrdtagl, @Tcrdtagw");
               comstr.AppendLine(" ,@tcrdw");

               comstr.AppendLine(" ,@Compcd,@Complot");
               comstr.AppendLine(" ,@Comppllt, convert(datetime,@Compexp,120),@compw ,@smpemp,@smpshtxt ");              
            }
            comstr.AppendLine(" , convert(datetime,@plndt,120),@itemno,UPPER(@topcode), @manufc,@leng, @ModifyBy, getdate(),@sample) ");

            CommandWrapper cmd = cnn.CreateCommand();
            cmd.CommandText = comstr.ToString();


            cmd.AddParameterStringWithValue("@topno", incoming._Topno.Trim());
            cmd.AddParameterStringWithValue("@plndt", DateUtil.ToInternalDate(incoming._Plandt, WinNemoLib.DateUtil.eLanguage.EN));
            cmd.AddParameterStringWithValue("@itemno", incoming._Itemno);
            cmd.AddParameterStringWithValue("@topcode", incoming._Topcrdcd);
            cmd.AddParameterStringWithValue("@manufc", incoming._Manufc);
            cmd.AddParameterStringWithValue("@leng", incoming._Length);
            cmd.AddParameterStringWithValue("@status", incoming._Plants);
            cmd.AddParameterStringWithValue("@sample", incoming._Sample);
            if (StrUtil.IsNotEmpty(incoming._Sample))
            {
               cmd.AddParameterStringWithValue("@runflag", incoming._Runflag);
               cmd.AddParameterStringWithValue("@Tcrdcd", incoming._Tcrdcd);
               cmd.AddParameterStringWithValue("@Tcrdlot", incoming._Tcrdlot);
               cmd.AddParameterStringWithValue("@tcrdexp", DateUtil.ToInternalDate(incoming._Tcrdexp, WinNemoLib.DateUtil.eLanguage.EN));
               cmd.AddParameterStringWithValue("@Tcrdtagl", incoming._Tcrdtagl);
               cmd.AddParameterStringWithValue("@Tcrdtagw", incoming._Tcrdtagw);
               cmd.AddParameterStringWithValue("@tcrdw", incoming._Tcrdw);
               cmd.AddParameterStringWithValue("@Compcd", incoming._Compcd);               
               cmd.AddParameterStringWithValue("@Complot", incoming._Complot);
               cmd.AddParameterStringWithValue("@Comppllt", incoming._Comppllt);
               cmd.AddParameterStringWithValue("@Compexp", DateUtil.ToInternalDate(incoming._Compexp, WinNemoLib.DateUtil.eLanguage.EN));
               cmd.AddParameterStringWithValue("@compw", incoming._Compw);
               cmd.AddParameterStringWithValue("@actflag", incoming._Actflag);
               cmd.AddParameterStringWithValue("@smpemp", incoming._smpemp);
               cmd.AddParameterStringWithValue("@smpshtxt", incoming._smpshtxt);
            }
            cmd.AddParameterStringWithValue("@ModifyBy", usernm);
            cmd.AddParameterStringWithValue("@shift", incoming._Shift);

            if (cmd.ExecuteNonQuery() <= 0)
            {
               cnn.RollbackTransaction();
               return false;
            }

            return true;
         }
         catch (Exception ex)
         {
            return false;
            throw ex;
         }
            
         }

      public Boolean DEL_DATA_NO_ACTIVE(ConnectionWrapper cnn,string datePt)
      {
         try
         {
            StringBuilder comstr = new StringBuilder();
            comstr.AppendLine(" DELETE ");
            comstr.AppendLine(Nemo.GetINI("Dbase", "Base") + ".[dbo].[TOPCORDPLAN]");
            comstr.AppendLine("where Plansts='U' and plandt=convert(datetime,@dt,120) ");

            CommandWrapper cmd = cnn.CreateCommand();
            cmd.CommandText = comstr.ToString();
            cmd.AddParameterStringWithValue("@dt", DateUtil.ToInternalDate(datePt, WinNemoLib.DateUtil.eLanguage.EN));


            cmd.ExecuteNonQuery();

            return true;
         }
         catch (Exception ex)
         {
            return false;
            throw ex;
         }
        
      }
      public Boolean UPDATE_ALL(TopCordPlan_DS incoming)
      {
         try
         {
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
               cnn.Open();
               cnn.BeginTransaction();

               StringBuilder comstr = new StringBuilder();
               comstr.AppendLine(" UPDATE ");
               comstr.AppendLine(Nemo.GetINI("Dbase", "Base") + ".[dbo].[TOPCORDPLAN] SET");

               comstr.AppendLine(" [topcrdcd]=@topcrdcd,[manufc]=@manufc,[length]=@length,[Plansts]=@Plansts,shift=@shift ");
               //comstr.AppendLine(" ,[actflag]=@actflag,[actdt]=convert(datetime,@actdt,120) ,[runflag]=@runflag ");
               //comstr.AppendLine(",[Rundat]= convert(datetime,@Rundat,120) , [finflag]=@finflag, [findat]=convert(datetime,@findat,120)  ");
               comstr.AppendLine(", [Tcrdcd]=@Tcrdcd, [Tcrdlot]=@Tcrdlot");
               comstr.AppendLine(" , [tcrdexp]=convert(datetime,@tcrdexp,120) , [Tcrdtagl]=@Tcrdtagl, [Tcrdtagw]=@Tcrdtagw");
               comstr.AppendLine(" , [tcrdw]=@tcrdw, [tauthemp]=@tauthemp, [treatxt]=@treatxt");

               comstr.AppendLine(" , [Compcd]=@Compcd, [tshtxt]=@tshtxt, [Complot]=@Complot");
               comstr.AppendLine(" , [Comppllt]=@Comppllt, [Compexp]=@Compexp, [compw]=@compw");
               comstr.AppendLine(" , [Cauthemp]=@Cauthemp, [creatxt]=@creatxt, [cshtxt]=@cshtxt");

               comstr.AppendLine(",[empname]= @empname ");
               comstr.AppendLine(" WHERE  [plandt]=convert(datetime,@plandt,120) and  [itemno]=@itemno and [topno]=@topno ");
           
               CommandWrapper cmd = cnn.CreateCommand();
               cmd.CommandText = comstr.ToString();

               cmd.AddParameterStringWithValue("@topcrdcd", incoming._Topcrdcd);
               cmd.AddParameterStringWithValue("@manufc", incoming._Manufc);
               cmd.AddParameterStringWithValue("@length", incoming._Length);
               cmd.AddParameterStringWithValue("@Plansts", incoming._Plants);
               //cmd.AddParameterStringWithValue("@actflag", incoming._Actflag);
               //cmd.AddParameterStringWithValue("@actdt", DateUtil.ToInternalDate(incoming._Actdt, WinNemoLib.DateUtil.eLanguage.EN));
               //cmd.AddParameterStringWithValue("@runflag", incoming._Runflag);
               //cmd.AddParameterStringWithValue("@Rundat", DateUtil.ToInternalDate(incoming._Rundt, WinNemoLib.DateUtil.eLanguage.EN));
               
               cmd.AddParameterStringWithValue("@shift", incoming._Shift);
               //cmd.AddParameterStringWithValue("@findat", DateUtil.ToInternalDate(incoming._Findt, WinNemoLib.DateUtil.eLanguage.EN));
               cmd.AddParameterStringWithValue("@Tcrdcd", incoming._Tcrdcd);
               cmd.AddParameterStringWithValue("@Tcrdlot", incoming._Tcrdlot);
               cmd.AddParameterStringWithValue("@tcrdexp", DateUtil.ToInternalDate(incoming._Tcrdexp, WinNemoLib.DateUtil.eLanguage.EN));
               cmd.AddParameterStringWithValue("@Tcrdtagl", incoming._Tcrdtagl);
               cmd.AddParameterStringWithValue("@Tcrdtagw", incoming._Tcrdtagw);
               cmd.AddParameterStringWithValue("@tcrdw", incoming._Tcrdw);
               cmd.AddParameterStringWithValue("@tauthemp", incoming._Tauthemp);
               cmd.AddParameterStringWithValue("@treatxt", incoming._Treatxt);
               cmd.AddParameterStringWithValue("@Compcd", incoming._Compcd);
               cmd.AddParameterStringWithValue("@tshtxt", incoming._Tshtxt);
               cmd.AddParameterStringWithValue("@Complot", incoming._Complot);
               cmd.AddParameterStringWithValue("@Comppllt", incoming._Comppllt);
               cmd.AddParameterStringWithValue("@Compexp", incoming._Compexp);
               cmd.AddParameterStringWithValue("@compw", incoming._Compw);
               cmd.AddParameterStringWithValue("@Cauthemp", incoming._Cauthemp);
               cmd.AddParameterStringWithValue("@creatxt", incoming._Creatxt);
               cmd.AddParameterStringWithValue("@cshtxt", incoming._Cshtxt);


               cmd.AddParameterStringWithValue("@itemno", incoming._Itemno);
               cmd.AddParameterStringWithValue("@topno", incoming._Topno);
               cmd.AddParameterStringWithValue("@plandt", DateUtil.ToInternalDate(incoming._Plandt, WinNemoLib.DateUtil.eLanguage.EN));
               cmd.AddParameterStringWithValue("@empname", incoming._Empname);

               
               if (cmd.ExecuteNonQuery() <= 0)
               {
                  cnn.RollbackTransaction();
                  return false;
               }

               cnn.CommitTransaction();
               return true;
            }
         }
         catch (Exception)
         {

            return false;
         }

      }

      public Boolean UPDATE(string usernm,string Status="",string SEQ = "-",string dtPlan="")
      {
         try
         {
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
               cnn.Open();
               cnn.BeginTransaction();

               StringBuilder comstr = new StringBuilder();
               comstr.AppendLine(" UPDATE ");
               comstr.AppendLine(Nemo.GetINI("Dbase", "Base") + ".[dbo].[TOPCORDPLAN] SET");
               if (Status == "A")
               {
                  comstr.AppendLine(" actflag='X' , actdt=getdate()  ,[empname]= @ModifyBy");
               }
               else if (Status == "R")
               {
                  comstr.AppendLine(" runflag='X' , Rundat=getdate() ");
               }
               else if (Status == "F")
               {
                  comstr.AppendLine(" finflag='X' , Findat=getdate() ");
               }
               else if ((Status == "L") ||(Status == "U"))
               {
                  comstr.AppendLine("Plansts='" + Status + "'");   
                  comstr.AppendLine(",[Uptemp]= @ModifyBy, [Uptdat]=getdate() ");            
               }

               

               if (Status == "L")
               {
                  comstr.AppendLine(" where Plansts='U' and isnull(actflag,'')='' and plandt = convert(datetime,@plndt,120) ");

               }
               if (Status == "U")
               {
                  string[] seqs = SEQ.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                  comstr.AppendLine(" where Plansts='L' and isnull(actflag,'')='' ");
                  comstr.AppendLine(" and  (convert(float,itemno) >= convert(float," + seqs[1] + ")");
                  comstr.AppendLine(" and plandt = convert(datetime,@plndt,120) ) ");
               }
               if ((Status == "A") || (Status == "R") || (Status == "F"))
               {
                  string[] seqs = SEQ.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                  comstr.AppendLine(" where Plansts='L' and plandt = convert(datetime,@plndt,120) ");
                  comstr.AppendLine(" and  convert(float,itemno) = convert(float," + seqs[1] + ")");

               }

               CommandWrapper cmd = cnn.CreateCommand();
               cmd.CommandText = comstr.ToString();
               if (Status == "U" || Status == "A" || (Status == "R") || Status == "F" )
               {
                  string[] seqs = SEQ.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                  cmd.AddParameterStringWithValue("@plndt", DateUtil.ToInternalDate(seqs[0], WinNemoLib.DateUtil.eLanguage.EN));
               }else if( Status=="L"){
                  cmd.AddParameterStringWithValue("@plndt", DateUtil.ToInternalDate(dtPlan, WinNemoLib.DateUtil.eLanguage.EN));
               }
            
               cmd.AddParameterStringWithValue("@ModifyBy", usernm);
               cmd.ExecuteNonQuery();


               cnn.CommitTransaction();
               return true;
            }
         }
         catch (Exception)
         {
            
            return false;
         }
         
      }

      public Boolean ROLLBACK_STATUS(string usernm, string Status = "", string SEQ = "-")
      {
         try
         {
            using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
               cnn.Open();
               cnn.BeginTransaction();

               StringBuilder comstr = new StringBuilder();
               comstr.AppendLine(" UPDATE ");
               comstr.AppendLine(Nemo.GetINI("Dbase", "Base") + ".[dbo].[TOPCORDPLAN] SET");
               if (Status == "A")
               {
                  comstr.AppendLine(" Tcrdcd=null ,Tcrdlot=null,tcrdexp=null ,Tcrdtagl=null,Tcrdtagw=null,tcrdw=null ,tauthemp=null,tshtxt=null ,treatxt=null");
                  comstr.AppendLine(" ,actflag=null , actdt=null  ,[empname]= @ModifyBy");
               }
               else if(Status == "A1")
               {
                  comstr.AppendLine(" Compcd=null,Complot=null,Comppllt=null,Compexp=null,compw=null,Cauthemp=null,creatxt=null,cshtxt=null ,actdt=getdate()  ,[empname]= @ModifyBy ");
               }

             
               if (Status == "A" || Status == "A1")
               {
                  string[] seqs = SEQ.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                  comstr.AppendLine(" where Plansts='L' and plandt = convert(datetime,@plndt,120) ");
                  comstr.AppendLine(" and  convert(float,itemno) = convert(float," + seqs[1] + ")");

               }

               CommandWrapper cmd = cnn.CreateCommand();
               cmd.CommandText = comstr.ToString();
               if ( Status == "A"  || Status=="A1")
               {
                  string[] seqs = SEQ.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                  cmd.AddParameterStringWithValue("@plndt", DateUtil.ToInternalDate(seqs[0], WinNemoLib.DateUtil.eLanguage.EN));
               }

               cmd.AddParameterStringWithValue("@ModifyBy", usernm);
               cmd.ExecuteNonQuery();
               //if ( <= 0)
               //{
               //   cnn.RollbackTransaction();
               //   return false;
               //}

               cnn.CommitTransaction();
               return true;
            }
         }
         catch (Exception)
         {

            return false;
         }

      }


      public Boolean SKIP_STATUS(string SEQ ,string autEmp,string txtshot)
      {
           string[] seqs = SEQ.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
          try
          {
              using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
              {
                  cnn.Open();
                  cnn.BeginTransaction();

                  StringBuilder comstr = new StringBuilder();
                  comstr.AppendLine(" UPDATE ");
                  comstr.AppendLine(Nemo.GetINI("Dbase", "Base") + ".[dbo].[TOPCORDPLAN] SET");
                  comstr.AppendLine(" Skpflag='X' , skpdt=getdate(),skpemp=@autEmp,skpshtxt=@skpshtxt ");
                  comstr.AppendLine(" ,finflag='X' , Findat=getdate(),actflag='X' , actdt=getdate()  ");
                  comstr.AppendLine(" where  plandt = convert(datetime,@plndt,120) ");
                  comstr.AppendLine(" and  convert(float,itemno) = convert(float," + seqs[1] + ")");

                  CommandWrapper cmd = cnn.CreateCommand();
                  cmd.CommandText = comstr.ToString();
                  
                     
                  cmd.AddParameterStringWithValue("@plndt", DateUtil.ToInternalDate(seqs[0], WinNemoLib.DateUtil.eLanguage.EN));
                  cmd.AddParameterStringWithValue("@autEmp", autEmp);
                  cmd.AddParameterStringWithValue("@skpshtxt", txtshot);

                  cmd.ExecuteNonQuery();
                  //if ( <= 0)
                  //{
                  //   cnn.RollbackTransaction();
                  //   return false;
                  //}

                  cnn.CommitTransaction();
                  return true;
              }
          }
          catch (Exception)
          {

              return false;
          }

      }
   }
}